include Bzflag


args = "1-30"

class TimeLimit < Plugin

  def initialize(args = nil)
    @args = args
    @save = bz_get_time_limit

    @allowed = allowed_times

    @timelimit_cmd = "/timelimit"
    @usage = "Usage : /timelimit <minutes>|show|reset"
  end

  def allowed_times

    if @args =~ /^[0-9]+-[0-9]+$/ 
      return (@args.split('-')[0]..@args.split('-')[1]).to_a

    elsif @args =~ /^[[:digit:]+,]+$/
      return @args.split(',')
    end

  end


  def player_join event_data 

    if bz_get_player_index_list.size == 1 && bz_is_time_manual_start? && !bz_is_count_down_active?  && !bz_is_count_down_in_progress?
      bz_set_time_limit(@save)
    end

  end

  def game_start event_data
    ids = bz_get_player_index_list
    ids.each do |id|
        bz_reset_player_score(id)
    end
  end 

  def game_end event_data 

    bz_set_time_limit(@save)

  end

  def unknown_slash_command event_data

    cmd = event_data.message.downcase.split[0]
    params = event_data.message.downcase.split[1..-1].to_s

#   puts "DEBUG :: params => #{params}"

    player_id = event_data.from

    if cmd != @timelimit_cmd
      return false
    else
      event_data.handled = true
    end

    if ! bz_has_perm?(player_id, "timelimit")
      bz_send_text_message(BZ_SERVER, player_id, "You do not have permission to run the timelimit command")
      return true
    end
      
    if ! bz_is_time_manual_start?
      bz_send_text_message(BZ_SERVER, player_id, "This server was not configured for manual clock countdowns")
      return true
    end

    if params.size == 0
      bz_send_text_message(BZ_SERVER, player_id, "#{@usage}")
      return true
    end

    if params == "show"
      bz_send_text_message(BZ_SERVER, player_id, "Match duration is set to #{bz_get_time_limit.round / 60} minute(s)")
      return true
    end
      
    if bz_is_count_down_in_progress?  
      bz_send_text_message(BZ_SERVER, player_id, "There is a countdown already in progress, match duration can't be changed now")
      return true
    elsif bz_is_count_down_active?
      bz_send_text_message(BZ_SERVER, player_id, "A game is already in progress, match duration can't be changed now")
      return true
    end

    player = Player.new(player_id)


    if params == "reset"
      bz_set_time_limit(@save)
      bz_send_text_message(BZ_SERVER, BZ_ALLUSERS, "Match duration reset to #{bz_get_time_limit.round / 60} minute(s) by #{player.callsign}")
      return true
    end

    curr = params

    if curr =~ /^[0-9]+$/

      if curr.to_i <= 0 || curr.to_i > 2880
        bz_send_text_message(BZ_SERVER, player_id, "Match duration can't be equal or lower then 0, or higher then 2880")
        return true
      end

      if @allowed == nil || @allowed.find { |l| l == curr} 
        bz_set_time_limit(curr.to_i * 60);
        bz_send_text_message(BZ_SERVER, BZ_ALLUSERS, "Match duration set to #{bz_get_time_limit.round / 60} minute(s) by #{player.callsign}")
      else
        bz_send_text_message(BZ_SERVER, player_id, "Not a valid match duration, valid match durations are : ")
        @allowed.each { |l| bz_send_text_message(BZ_SERVER, player_id, "* #{l} minute(s)") }
      end
    else
      bz_send_text_message(BZ_SERVER, player_id, "Not a correct value")
    end

    return true

  end

end



puts "YABZS :: timelimit"
t = TimeLimit.new(args)
t.register
